//
//  TalkfunExamView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/1/14.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TalkfunExamView: TalkfunHitTestView {
     /**考试列表*/
    @objc public let examinationView = TalkfunExaminationView()
    /**考试详情*/
    @objc public let testPaperDetails = TalkfunTestPaperDetails()
    /**简介*/
    @objc public let introductionToTestPapers = TalkFunIntroductionToTestPapers()
    //试卷主题的id
    @objc public var ID:NSInteger = 0
   
    @objc public var backgroundColorView = UIView()
    
    @objc public let view = TalkfunHitTestView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColorView.isHidden = true
        self.addSubview(self.backgroundColorView)
        self.backgroundColorView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
        
        self.addSubview(self.view)
        
        self.view.addSubview(self.examinationView)
        self.view.addSubview(self.testPaperDetails)
        self.view.addSubview(self.introductionToTestPapers)
    }
    func isiPhoneXScreen() -> Bool {
        if #available(iOS 11, *) {
                 guard let w = UIApplication.shared.delegate?.window, let unwrapedWindow = w else {
                     return false
                 }
                 
                 if unwrapedWindow.safeAreaInsets.left > 0 || unwrapedWindow.safeAreaInsets.bottom > 0 {
                     print(unwrapedWindow.safeAreaInsets)
                     return true
                 }
           }
           return false
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.backgroundColorView.frame = self.bounds
        
        var examinationViewH = self.frame.size.height * 0.71
        if (self.frame.width > self.frame.height) {
            examinationViewH = self.frame.size.height * 0.85
        }
        self.view.frame = CGRect(x: 0, y: self.frame.size.height - examinationViewH, width: self.frame.size.width, height: examinationViewH)
        

        
        if (self.examinationView.frame.width == 0) {
            self.initUI()
        }else
        {
            var frameWidth = self.frame.width
            var frameX = 0.0
            if (self.isiPhoneXScreen() && self.view.frame.width > self.view.frame.height) {
                
                frameX = 32.0
                frameWidth = self.frame.width - frameX
            }
            
            if(self.examinationView.frame.width != frameWidth)
            {
                //列表
                var examinationViewY = self.examinationView.frame.origin.y
                if (self.examinationView.YEqualsHigh) {
                    examinationViewY = self.view.frame.height
                }
                self.examinationView.frame = CGRect(x: frameX, y: examinationViewY, width: frameWidth, height: self.view.frame.height)
                //简介
                var introductionToTestPapersY = self.introductionToTestPapers.frame.origin.y
                if (self.introductionToTestPapers.YEqualsHigh) {
                    introductionToTestPapersY = self.view.frame.height
                }
                self.introductionToTestPapers.frame = CGRect(x: frameX, y: introductionToTestPapersY, width: frameWidth, height: self.view.frame.height)
                
                //详情
                var testPaperDetailsY = self.testPaperDetails.frame.origin.y
                if (self.testPaperDetails.YEqualsHigh) {
                    testPaperDetailsY = self.view.frame.height
                }
                self.testPaperDetails.frame = CGRect(x: frameX, y: testPaperDetailsY, width: frameWidth, height: self.view.frame.height)
                

            }
   
        }
        
       
    }
  
    @objc public func initUI()
    
    {
        var frameWidth = self.frame.width
        var frameX = 0.0
        if (self.isiPhoneXScreen() && self.view.frame.width > self.view.frame.height) {
            
            frameX = 32.0
            frameWidth = self.view.frame.width - frameX
        }
        self.examinationView.frame = CGRect(x: frameX, y: self.frame.size.height, width: frameWidth, height: self.view.frame.height)
        self.testPaperDetails.frame = self.examinationView.frame
        self.introductionToTestPapers.frame = self.examinationView.frame
       
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        self.backgroundColorView.isHidden = true
        self.examinationView.closeClick()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc public func examinationEnd(model:TFQuestionnaireEndResult,target:NSObject, action:Selector)
    {
        var answerStatus:TalkfunAnswerStatus = TalkfunAnswerStatusUnsubmitted
        
        for i in stride(from: 0, to: self.examinationView.dataList.count ,by: 1) {
            let data = self.examinationView.dataList[i] as! TalkfunQuestionnaire
           //列表中对比
            if(data.id  == model.id )
            {
                answerStatus = data.answerStatus;
            }
            
        }


                //未参加
                if (answerStatus == TalkfunAnswerStatusUnsubmitted) {
                    
                    //已经打开了详情页 &&  是 id相待  || 已经关闭了详情页
                    if ((self.testPaperDetails.YEqualsHigh == false && self.testPaperDetails.model?.subjectExamineInfo.id == model.id) || self.testPaperDetails.YEqualsHigh == true) {
                        TalkfunAlertNewView.shareInstance(type: TalkfunAlertNewView.TalkfunAlertType.endAnswerAndTheStudentHasSubmitted, target: target, action: action, ID: model.id)
                        //讲师已结束作答
                        
                    }
                    else
                    {
                        //已经关闭了详情页 ||  不相对id
                        
                    
                        TalkfunAlertNewView.shareInstance(type: TalkfunAlertNewView.TalkfunAlertType.otherTestQuestionsEndedEarlyAndTheStudentHasSubmitted, target: target, action: action, ID: model.id)
                        
                        //另一个测试讲师已结束作答
                    }
                    

                }else
                {
                    
                    //已经打开了详情页 &&  是 id相待  || 已经关闭了详情页
                    if ((self.testPaperDetails.YEqualsHigh == false && self.testPaperDetails.model?.subjectExamineInfo.id == model.id) || self.testPaperDetails.YEqualsHigh == true ) {
                        
                        //表单结束
                        if model.type == TFExaminationTypeTable {
                            TalkfunAlertNewView.shareInstance(type: TalkfunAlertNewView.TalkfunAlertType.endTheQuestionnaireEarly, target: target, action: action, ID: model.id)
                        }else
                        {
                            TalkfunAlertNewView.shareInstance(type: TalkfunAlertNewView.TalkfunAlertType.endAnswer, target: target, action: action, ID: model.id)
                        }
                        
                        //已经关闭了详情页
                        //讲师已结束作答  (试题已自动提交)
                      
                        
                    }else
                    {
                        
                        TalkfunAlertNewView.shareInstance(type: TalkfunAlertNewView.TalkfunAlertType.otherTestQuestionsEndedEarly, target: target, action: action, ID: model.id)
                        
                        //另一个测试讲师已结束作答     (试题已自动提交)
                    }
                    
                  

                }


    }
    


}
